package com.kate.mall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kate.mall.common.utils.PageUtils;
import com.kate.mall.common.utils.Query;

import com.kate.mall.product.dao.GoodsDao;
import com.kate.mall.product.entity.GoodsEntity;
import com.kate.mall.product.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                generatorWrapper(params)
        );

        return new PageUtils(page);
    }
    private  QueryWrapper<GoodsEntity> generatorWrapper(Map<String, Object> params){
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
        if(params.containsKey("isNew")){
            queryWrapper.lambda().eq(GoodsEntity::getIsNew,Integer.parseInt(String.valueOf(params.get("isNew"))));
        }
        if(params.containsKey("isHot")){
            queryWrapper.lambda().eq(GoodsEntity::getIsHot,Integer.parseInt(String.valueOf(params.get("isHot"))));
        }
        if(params.containsKey("categoryId")){
            queryWrapper.lambda().eq(GoodsEntity::getCategoryId,
                    Integer.parseInt(String.valueOf(params.get("categoryId"))));
        }

        //TODO 要不要写逻辑删除条件？？？
        return queryWrapper;
    }


    /**
     * 根据二级分类id列表查询所有商品列表
     * @param l2List    二级分类id列表
     * @param offset
     * @param limit
     * @return
     */

    public synchronized List<GoodsEntity> queryByCategory(List<Integer> l2List, int offset, int limit) {
        List<GoodsEntity> records = null;

        records = baseMapper.selectPage(new Page<GoodsEntity>(offset, limit), Wrappers.<GoodsEntity>lambdaQuery().
                in(GoodsEntity::getCategoryId, l2List).
                eq(GoodsEntity::getIsOnSale, true).
                eq(GoodsEntity::getDeleted, false).
                orderByDesc(GoodsEntity::getAddTime)).getRecords();
        return records;
    }
}